package com.sugar.Stack;

/*
* 使用链表实现栈
* 类似于链表中的头插法
* */
public class LinkedListStack {
    private Node top;

    public void push(int data){
        Node newNode = new Node(data);
        newNode.next = top;
        top = newNode;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("Stack is empty");
        }
        int element = top.data;
        top = top.next;
        return element;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("Stack is empty");
        }
        return top.data;
    }

    private boolean isEmpty(){
        return top==null;
    }
}

class Node{
    int data;
    Node next;
    public Node(int data){
        this.data = data;
    }
}
